package com.xca.ninedemo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.xca.ninedemo.R;
import com.xca.ninedemo.entity.TopNews;

import java.util.List;

public class TopNewsAdapter extends RecyclerView.Adapter<TopNewsAdapter.ViewHolder>{

    private List<TopNews.Result.Data> mList ;
    private Context context ;
    private Activity activity ;

    public TopNewsAdapter(List<TopNews.Result.Data> list , Context context , Activity activity)
    {
        this.mList = list ;
        this.context = context ;
        this.activity = activity ;
    }

    public void updateDate(List<TopNews.Result.Data> list)
    {
        mList.clear();
        this.mList = list ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context , R.layout.item_news , null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TopNews.Result.Data data = mList.get(position);
        if (data != null){
            holder.title.setText(data.getTitle());
            holder.time.setText(data.getDate());
            holder.author.setText(data.getAuthor_name());
            String icon1 = data.getThumbnail_pic_s();
            String icon2 = data.getThumbnail_pic_s02();
            String icon3 = data.getThumbnail_pic_s03();
            holder.linearLayout.setVisibility(View.GONE);

            if (icon1 != null && !icon1.isEmpty()){
                Picasso.get()
                        .load(icon1)
                        .into(holder.imageView1);
            }else {
                holder.imageView1.setVisibility(View.GONE);
            }

            /*if (icon1 == null){
                holder.imageView1.setVisibility(View.GONE);
                holder.linearLayout.setVisibility(View.GONE);
                return;
            }
            if (icon1 != null && !icon1.isEmpty() && icon2 == null){
                Picasso.get()
                        .load(icon1)
                        .into(holder.imageView1);
                holder.linearLayout.setVisibility(View.GONE);
                return;
            }
            if (icon2 != null && !icon2.isEmpty() && icon3 == null){
                holder.imageView1.setVisibility(View.GONE);
                Picasso.get()
                        .load(icon1)
                        .into(holder.imageView2);
                Picasso.get()
                        .load(icon2)
                        .into(holder.imageView3);
                holder.imageView4.setVisibility(View.INVISIBLE);
                return;
            }
            if (icon3 != null && !icon3.isEmpty()){
                holder.imageView1.setVisibility(View.GONE);
                Picasso.get()
                        .load(icon1)
                        .into(holder.imageView2);
                Picasso.get()
                        .load(icon2)
                        .into(holder.imageView3);
                Picasso.get()
                        .load(icon3)
                        .into(holder.imageView4);
                return;
            }*/

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openWebsite(data.getUrl());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView title , time , author;
        ImageView imageView1 , imageView2 , imageView3 , imageView4  ;
        LinearLayout linearLayout ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.wi_title);
            time = itemView.findViewById(R.id.wi_time);
            author = itemView.findViewById(R.id.wi_author);
            imageView1 = itemView.findViewById(R.id.wi_img1);
            imageView2 = itemView.findViewById(R.id.wi_img2);
            imageView3 = itemView.findViewById(R.id.wi_img3);
            imageView4 = itemView.findViewById(R.id.wi_img4);
            linearLayout = itemView.findViewById(R.id.wi_layout);
        }
    }

    private void openWebsite(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "无法打开网页，请检查设备是否安装了浏览器", Toast.LENGTH_SHORT).show();
        }
    }
}
